<?php
namespace app\reduction\model;

use app\BaseModel;
use app\commission\model\Cash;
use app\commission\model\Water;
use think\facade\Db;

class PayRecord extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_reduction_pay_record';


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 11:53
     * @功能说明:查询数量
     */
    public function recordCount($dis){

        $count = $this->where($dis)->count();

        return $count;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 14:45
     * @功能说明:领取
     */
    public function addRecord($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-04-21 10:06
     * @功能说明:活动详情
     */
    public function recordInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():$data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 10:07
     * @功能说明:活动编辑
     */
    public function recordUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 15:02
     * @功能说明:列表
     */
    public function recordList($dis,$page){

        $data = $this->alias('a')
                ->join('longbing_card_user b','a.user_id = b.id','left')
                ->join('longbing_card_user_info c','a.staff_id = c.fans_id','left')
                ->where($dis)
                ->field(['a.*','b.nickName','b.avatarUrl','c.name as staff_name'])
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){
                //头像
                $v['avatarUrl'] = !empty($v['avatarUrl'])?$v['avatarUrl']:'https://lbqnyv2.migugu.com/defaultAvatar.png';
                //员工名称
                $v['staff_name']= !empty($v['staff_name'])?$v['staff_name']:Db::name('longbing_card_user')->where(['id'=>$v['staff_id']])->value('nickName');
            }
        }
        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-23 13:20
     * @功能说明:增加流水
     */
    public function addWater($order_code,$coupon){
        //查找订单
        $order = $this->recordInfo(['order_code'=>$order_code]);

        if(empty($order)){

            return true;
        }

        $data = [

            'uniacid'   => $order['uniacid'],

            'source_id' => $order['user_id'],

            'user_id'   => $coupon['staff_id'],

            'target_id' => $coupon['id'],

            'type'      => 0,

            'balance'   => 0,

            'cash'      => $coupon['cash'],

            'selling_type' => 0,

            'status'    => 0,

            'num'       => 1,

            'order_id'  => $order['id'],

            'order_code'=> $order['order_code']

        ];

        $water_model = new Water();
        //增加流水
        $res = $water_model->addWater($data);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-23 11:30
     * @功能说明:支付回调
     */
    public function payResult($order_code,$transaction_id){

        $order = $this->recordInfo(['order_code'=>$order_code]);

        if(!empty($order)&&$order['status']==0){

            //修改状态
            $this->recordUpdate(['id'=>$order['id']],['status'=>2,'pay_time'=>time(),'transaction_id'=>$transaction_id]);
            //佣金流水模型
            $water_model = new Water();
            //用户钱包模型
            $cash_model  = new Cash();
            //查找这笔订单有无流水
            $water_info  = $water_model->waterInfo(['order_id'=>$order['id'],'status'=>0]);

            if(!empty($water_info)){
                //修改流水状态
                $water_model->waterUpdate(['id'=>$water_info['id']],['status'=>2]);
                //增加用户的佣金
                $dis = [
                    'uniacid' => $water_info['uniacid'],

                    'user_id' => $water_info['user_id'],
                ];
                $cash_model->incOrDecCash($dis,$water_info['cash']);

            }
            //抵扣优惠券
            if(!empty($order['coupon_id'])){

                $user_record = new UserRecord();

                $user_record->recordUpdate(['id'=>$order['coupon_id']],['status'=>2,'use_time'=>time(),'pay_price'=>$order['pay_price']]);
            }
        }
        return true;
    }










}